package com.ez2find.backing;

import com.ez2find.manager.BackupManagerLocal;
import com.ez2find.manager.DirectoryManagerLocal;
import com.ez2find.model.Backup;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

public class BackupBean {

    @EJB
    BackupManagerLocal ejbBackups;
    @EJB
    DirectoryManagerLocal ejbDirectories;

    public BackupBean() {
    }

    public String createBackup() throws IOException {
        String[] command = {"sh", "-c", "mysqldump -uez2find -pez2find ez2find > " + ejbDirectories.listAll().get(0).getName() + "eez2find.sql"};
        Runtime.getRuntime().exec(command);
        FacesMessage msg = new FacesMessage("Backup created succesfully in /home/backup/eez2find.sql");
        msg.setSeverity(FacesMessage.SEVERITY_ERROR);
        FacesContext.getCurrentInstance().addMessage(null, msg);
        return null;
    }

}
